package ui.control
{
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	
	import ui.GuiWnd;
	import ui.control.controlStruct.TagGuiListItem;

	public class GuiList extends GuiStatic
	{
		
		public static const LEFT:String = "Left";
		public static const CENTER:String = "Center";
		public static const RIGHT:String = "Right";
		
		private var listContainer:GuiWnd;
		private var itemList:Array;
		private var itemSize:Array;
		
		private var itemWidth:int;		//默认列宽度
		private var itemHeight:int; 	//行高
		private var itemAlign:String;//默认对齐方式

		private var listWidth:int;
		private var listHeight:int;

		public function GuiList()
		{
			listContainer = new GuiWnd();
			
			addChild(listContainer);
			
			itemList = new Array();
			itemSize = new Array();
			
			itemWidth = 10;
			itemAlign = LEFT;
			itemHeight = 16;
			
			
			setSize(100,100)
			
		}
		
		/**
		 * 添加列 
		 * @param col  		列号
		 * @param nWidth 	宽度
		 * @param align 	对齐方式
		 * 
		 */		
		public function insertColumn(col:int, nWidth:int,align:String = LEFT):void
		{
			itemSize[col] = {itemWidth:nWidth,itemAign:align};
		}
		
		/**
		 * 添加item
		 * @param row   行
		 * @param col	列
		 * @param sprObj 显示对像
		 * 
		 */		
		public function insertItem(row:int, col:int, sprObj:Sprite):void
		{
			var item:TagGuiListItem = itemList[row] as TagGuiListItem;
			
			var itemSize:Object = itemSize[col];
			
			if(!item)
			{
				item  = new TagGuiListItem();
				itemList[row] = item;
				
				listContainer.addChild(item);
			}
			
			var itemSprite:GuiWnd = new GuiWnd();
			
			
			var w:int = itemWidth;
			
			if(itemSize)
			{
				w = itemSize.itemWidth;
			}
			
			itemSprite.scrollRect = new Rectangle(0, 0, w, itemHeight);
			
			
			var strAlign:String = itemAlign;
			
			if(itemSize)
			{
				strAlign = itemSize.itemAign;
			}
			
			switch(strAlign)
			{
				case LEFT:
					sprObj.x = 0;
					break;
				case CENTER:
					sprObj.x = w - sprObj.width >> 1;
					break;
				case RIGHT:
					sprObj.x = w - sprObj.width;
					break;
			}
			
			itemSprite.addChild(sprObj);
			itemSprite.x = getColX(col);
			
			item.PushCol(col, itemSprite);
			
			updataList();
		}
		
		public function getItem(row:int,col:int):Object
		{
			var item:TagGuiListItem = itemList[row] as TagGuiListItem;
			
			if(item)
			{
				return item.itemLIst[col];
			}
			
			return null;
		}
		
		public function deleteRow(row:int):void
		{	
			itemList.splice(row,1);
			updataList();
		}
		
		/**
		 * 更新列表 
		 * 
		 */		
		private function updataList():void
		{
			var n:int = 0;
			for(var i:int=0; i<itemList.length; i++)
			{
				var item:TagGuiListItem = itemList[i] as TagGuiListItem;
				if(item)
				{
					item.y = n * itemHeight;
					n++;
				}
			}
		}
		
		public function getListNum():int
		{
			var n:int = 0;
			
			for(var i:int=0; i<itemList.length; i++)
			{
				var item:TagGuiListItem = itemList[i] as TagGuiListItem;
				if(item)
				{
					n++;
				}
			}
			
			return n;
		}
		
		/**
		 * 获取某列的水平坐标 
		 * @param col 列号
		 * @return 
		 * 
		 */		
		private function getColX(col:int):int
		{
			var posX:int = 0;
			
			var num:int = col;
			
			for(var i:int=0; i<num; i++)
			{
				var obj:Object = itemSize[i];
				var w:int = itemWidth;
				if(obj)
				{
					w = obj.itemWidth;
				}
				
				posX += w;
			}
			
			return posX;
		}
		
		public override function set width(value:Number):void
		{
			listWidth = value;
			updataScrollRect();
		}
		
		
		public override function set height(value:Number):void
		{
			listHeight = value;
			updataScrollRect();
		}
		
		public function setSize(nWidth:int, nHeight:int):void
		{
			listWidth = nWidth;
			listHeight = nHeight;
			
			updataScrollRect();
		}
		
		public function updataScrollRect():void
		{
			this.scrollRect = null;
			this.scrollRect = new Rectangle(0, 0, listWidth, listHeight);
		}
		
		public function clear():void
		{
			for(var i:int=0; i<itemList.length; i++)
			{
				var item:TagGuiListItem = itemList[i] as TagGuiListItem;
				if(item)
				{
					listContainer.removeChild(item);
					item.destroy();
					item = null;
				}
			}
			itemList.splice(0);
			
		}
		
	}
}